package com.awesome.todo.activities;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.awesome.todo.R;
import com.awesome.todo.User;
import com.awesome.todo.database.UserDataSource;

/**
 * This activity is used to register a new account.
 * 
 * @author Doug Black
 * 
 */
public class NewUserActivity extends Activity {

	public EditText nameEdit;
	public EditText emailEdit;
	public EditText loginEdit;
	public EditText passwordEdit1;
	public EditText passwordEdit2;
	public Button newUserButton;
	public Button cancelButton;
	public UserDataSource userTable;
	

	/**
	 * This onCreate method simply hooks up the interface
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newuser);
		
		userTable = new UserDataSource(this.getApplicationContext());
		
		nameEdit = (EditText) findViewById(R.id.nameEditText);
		emailEdit = (EditText) findViewById(R.id.emailEditText);
		loginEdit = (EditText) findViewById(R.id.loginEditText);
		passwordEdit1 = (EditText) findViewById(R.id.passwordEditText1);
		passwordEdit2 = (EditText) findViewById(R.id.passwordEditText2);
		newUserButton = (Button) findViewById(R.id.newUserButton);
		cancelButton = (Button) findViewById(R.id.cancelButton);
		
		newUserButton.setOnClickListener(new OnClickListener() {

			/**
			 * This is called when the user clicks the "sign up" button. It
			 * verifies that the entered valued are valid and then creates and
			 * stores the new user.
			 */
			public void onClick(View arg0) {

				String name = nameEdit.getText().toString().trim();
				String email = emailEdit.getText().toString().trim();
				String login = loginEdit.getText().toString().trim();
				String password1 = passwordEdit1.getText().toString().trim();
				String password2 = passwordEdit2.getText().toString().trim();

				// VERIFY VALUES ARE ACCURATE

				if (User.validEmail(email) && password1.equals(password2)) {
					userTable.createUser(name, email, login, password1);
					Log.i(NewUserActivity.class.toString(), "SUCCESSFULLY ADDED USER");
				} else {
					// ERROR DIALOG
				}

				Log.i("ONCLICK", "CLICKED");
				finish();

			}

		});
		
		cancelButton.setOnClickListener(new OnClickListener() {
			
			/**
			 * Cancels sign up and closes the activity.
			 */
			public void onClick(View v) {
				finish();
			}
			
		});

	}

}